package com.smarthealthring.mm.widgets;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.smarthealthring.mm.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jakewharton.rxbinding2.view.RxView;


/**
 * Created by hewei26 on 2017/7/14.
 */

public class SwipeRecyclerView extends RelativeLayout {

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private BaseQuickAdapter mAdapter;

    private View mEmptyView;
    private View mErrorView;

    public SwipeRecyclerView(Context context) {
        this(context, null);
    }

    public SwipeRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SwipeRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.swipe_recyclerview, this);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);

        mEmptyView = findViewById(R.id.base_ll_empty);
        mErrorView = findViewById(R.id.base_ll_error);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(context, R.color.colorAccent));
    }

    public RecyclerView getRecView(){
        return mRecyclerView;
    }


    public void setAdapter(BaseQuickAdapter adapter){

        RxView.clicks(mEmptyView)
                .subscribe(o -> {
                    startRefresh();
                });

        RxView.clicks(mErrorView)
                .subscribe(o -> {
                    startRefresh();
                });

        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            startRefresh();
        });


        mAdapter = adapter;
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnLoadMoreListener(() -> {
            enableRefresh(false);
            mListener.onLoadMore();
        }, mRecyclerView);

    }

    public void enableRefresh(boolean enableRefresh) {
        mSwipeRefreshLayout.setEnabled(enableRefresh);
    }

    public void enableLoadMore(boolean enableLoadMore) {
        mAdapter.setEnableLoadMore(enableLoadMore);
    }


    /**
     * 开始刷新
     */
    public void startRefresh() {
        showContent();
        enableLoadMore(false);

        mRecyclerView.smoothScrollToPosition(0);
        mSwipeRefreshLayout.setRefreshing(true);

        if (mListener != null) {
            mListener.onRefresh();
        }
    }

    /**
     * 结束刷新
     */
    public void stopRefresh(){
        mSwipeRefreshLayout.setRefreshing(false);
    }


    private IRecyclerViewListener mListener;

    public void setOnIRecyclerViewListener(IRecyclerViewListener listener) {
        mListener = listener;
    }

    public interface IRecyclerViewListener {

        void onRefresh();

        void onLoadMore();
    }

    public void showRefreshing(){
        showContent();
        enableLoadMore(false);

        mRecyclerView.smoothScrollToPosition(0);
        mSwipeRefreshLayout.setRefreshing(true);
    }

    public void showContent() {
        mSwipeRefreshLayout.setVisibility(View.VISIBLE);
        mEmptyView.setVisibility(View.INVISIBLE);
        mErrorView.setVisibility(View.INVISIBLE);
    }


    public void refreshComplete(){
        stopRefresh();
//        enableLoadMore(true);
    }

    public void refreshEmpty(){
        mEmptyView.setVisibility(View.VISIBLE);
        mErrorView.setVisibility(View.INVISIBLE);
        mSwipeRefreshLayout.setVisibility(View.GONE);

        mRecyclerView.removeAllViews();
        enableLoadMore(true);
    }

    public void refreshFail(){
        mErrorView.setVisibility(View.VISIBLE);
        mEmptyView.setVisibility(View.INVISIBLE);
        mSwipeRefreshLayout.setVisibility(View.GONE);

        mRecyclerView.removeAllViews();
        enableLoadMore(true);
    }


    public void loadMoreEnd(){
        mAdapter.loadMoreEnd();
        enableRefresh(true);
    }

    public void loadMoreComplete(){
        mAdapter.loadMoreComplete();
        enableRefresh(true);
    }

    public void loadMoreFail(){
        mAdapter.loadMoreFail();
        enableRefresh(true);
    }
}
